import json,sys,os,random
import thread
import threading
import LiveDeal

def func(sts):
  print len(sts)
  while 1:
    for st in sts:
      seq = st.split()
      res=LiveDeal.loadData(seq[0], True)
      if len(res)==0:
        #print "err, ", seq[0]
        continue
      keys=res.keys()
      keys.sort()
      tc=1
      #print seq[0] 
      while tc<2000:
        f=LiveDeal.checkPoint(2000-tc, keys, res)
        if f:
          print "buy ", st
          break
        tc+=1
        #break
    break
  print "## thread end"
f=open("../stocks")
lines = f.readlines()
stocks=list()
for i in lines:
  i=i.strip()
  if len(i) > 5 and i not in stocks:
    stocks.append(i)
random.shuffle(stocks)
#stocks=['600191']
n=10
l=len(stocks)/n
c=0
threads=list()
while c<n:
  sts=stocks[c*l:(c+1)*l]
  #t=thread.start_new(func, (sts,))
  th = threading.Thread(target=func,args=(sts,));
  threads.append(th)
  th.start()
  c+=1

print "start"
for th in threads:
  th.join()
print "end"
sys.exit()
